package net.huashitong.supervision.task.controller;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.service.*;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.dao.TaskUnitDao;
import net.huashitong.supervision.task.dataBean.TaskUnitBean;
import net.huashitong.supervision.task.entity.TaskUnit;
import net.huashitong.supervision.task.filter.TaskUnitFilter;
import net.huashitong.supervision.task.service.CopeToService;
import net.huashitong.supervision.task.service.TaskMainService;
import net.huashitong.supervision.task.service.TaskUnitService;
import net.huashitong.supervision.task.service.TaskUserService;
import net.huashitong.synthetical.newSupervision.service.TaskMessageService;
import net.huashitong.synthetical.newSupervision.service.TaskNewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.DecimalFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/9/10
 * Time: 10:32
 * To change this template use File | Settings | File Templates.
 * Description:督查任务统计报表
 */
@Controller
@RequestMapping(value = "/table")
public class TableController extends JsonBaseController {
    @Autowired
    private SysUserPermissionService userPermissionService;
    @Autowired
    private SysRolePermissionService rolePermissionService;
    @Autowired
    private SysMenuService sysMenuService;
    @Autowired
    private SysLoginService loginService;
    @Autowired
    private SysUserLoginService sysUserLoginService;
    @Autowired
    private SysDeptService sysDeptService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private TaskNewService taskNewService;
    @Autowired
    private TaskMessageService taskMessageService;
    @Autowired
    private SysLoginService sysLoginService;
    @Autowired
    private TaskMainService taskMainService;
    @Autowired
    private TaskUnitService taskUnitService;
    @Autowired
    private TaskUserService taskUserService;
    @Autowired
    private CopeToService copeToService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    protected String getView(String view) {
        return "/modules/task/table/" + view;
    }

    /**
     * 返回报表页面
     *
     * @return
     */
    @RequestMapping(value = "/showDuCha")
    public String showDucha(Model model, String deptId) {
        SysLogin sysLogin = sysLoginService.getById(SysUserUtils.getUserId());
        SysDept dept = sysDeptService.getById(sysLogin.getDeptId());
        SysUser sysUser = sysUserService.getById(SysUserUtils.getUserId());
        List<Map> dcMapList = new ArrayList<>();
        List<Map<String, Object>> xiangzhendwMapList = new ArrayList<>();
        List<Map<String, Object>> xianzhidwMapList = new ArrayList<>();
        DecimalFormat df=new DecimalFormat("#.00");
        //当前单位分数
        int deptScore = 0;
        String deptWCL="0%";
        //当前单位排名
        int deptSort = 1;
        //报表
        //如果当前用户是督查组
       /* if ("02".equals(sysLogin.getRoleId())) {
            //如果是单位领导
        } else if ("03".equals(sysLogin.getRoleId())) {
            //如果是分管，查看当前单位的所有分管的任务
            SysUserFilter sysUserFilter = new SysUserFilter();
            sysUserFilter.setRoleId("03");
            sysUserFilter.setDeptId(dept.getSysId());
            List<SysLogin> loginList = sysLoginService.queryUserLoginByRoleDept(sysUserFilter);
            for (SysLogin login : loginList) {
                   *//* //查看分管的任务数
                    TaskUserFilter taskUserFilter = new TaskUserFilter();
                    taskUserFilter.setUnitUserId(login.getSysId());
                    List<TaskUser> allTask = taskUserService.queryFilter(taskUserFilter);
                    taskUserFilter.setStatus("3");
                    //重办任务
                    List<TaskUser> cbTask = taskUserService.queryFilter(taskUserFilter);
                    int cb = cbTask.size();
                    int all = allTask.size();
                    Map<String,String>map=new HashMap<>();
                    SysUser fgUser = sysUserService.getById(login.getSysId());
                    map.put("name",fgUser.getName());
                    map.put("all", all+"");
                    map.put("cb",cb+"");
                    dcMapList.add(map);*//*
            }
        } else if ("04".equals(sysLogin.getRoleId())) {
        }*/
        //如果是乡镇单位取出说有乡镇单位

        List<SysDept> list1 = deptService.queryDeptList("/02/201/");
        for (SysDept sysDept : list1) {
            //根据当前单位查询该单位的任务
            Map<String, Object> map = new HashMap<>();
            TaskUnitFilter taskUnitFilter = new TaskUnitFilter();
            taskUnitFilter.setDeptId(sysDept.getSysId());
            taskUnitFilter.setCategoryId("20191022100000000002");
            //任务数
            int allSize = taskUnitService.queryByCategory(taskUnitFilter).size();
            taskUnitFilter.setType("3");
            //重办数
            int cbSize = taskUnitService.queryByCategory(taskUnitFilter).size();
            //已完成数
            taskUnitFilter.setType("4");
            List<TaskUnitBean> taskUnitList = taskUnitService.queryByCategory(taskUnitFilter);
            int ywcSize = taskUnitList.size();
            //得分
            int scores = 0;
            for (TaskUnitBean taskUnit : taskUnitList) {
                if (StringUtils.isNotBlank(taskUnit.getScore())) {
                    int score = Integer.parseInt(taskUnit.getScore());
                    scores += score;
                }
            }
            //逾期数
            taskUnitFilter.setType(null);
            taskUnitFilter.setStatus("10");
            int yqSize = taskUnitService.queryByCategory(taskUnitFilter).size();
            //完成率
            double d=0;
            if(ywcSize>0){
                String format = df.format((float) ywcSize / (float) allSize * 100);
                d = Double.parseDouble(format);
            }
            map.put("allSize", allSize + "");
            map.put("cbSize", cbSize + "");
            map.put("ywcSize", ywcSize + "");
            map.put("yqSize", yqSize + "");
            map.put("deptName", sysDept.getDeptName());
            map.put("scores", scores);//单位总分
            map.put("wcl", d);
            xiangzhendwMapList.add(map);
        }

        if (xiangzhendwMapList != null && xiangzhendwMapList.size() > 1) {
            Collections.sort(xiangzhendwMapList, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Double o1Value = Double.valueOf(o1.get("wcl").toString());
                    Double o2Value = Double.valueOf(o2.get("wcl").toString());
                    return o2Value.compareTo(o1Value);
                }
            });
        }
        //查询当前单位的分数和排名
        for (int i = 0; i < xiangzhendwMapList.size(); i++) {
            String deptName = (String) xiangzhendwMapList.get(i).get("deptName");
            if (dept.getDeptName().equals(deptName)) {
                deptScore = (int) xiangzhendwMapList.get(i).get("scores");
                deptWCL = xiangzhendwMapList.get(i).get("wcl")+"";
                deptSort = i + 1;
            }
        }
        //如果是县直单位取出说有县直单位
        List<SysDept> list = deptService.queryDeptList("/02/202/");
        for (SysDept sysDept : list) {
            //根据当前单位查询该单位的任务
            Map<String, Object> map = new HashMap<>();
            TaskUnitFilter taskUnitFilter = new TaskUnitFilter();
            taskUnitFilter.setCategoryId("20191022100000000002");
            taskUnitFilter.setDeptId(sysDept.getSysId());
            //任务数
            int allSize = taskUnitService.queryByCategory(taskUnitFilter).size();
            taskUnitFilter.setType("3");
            //重办数
            int cbSize = taskUnitService.queryByCategory(taskUnitFilter).size();
            //已完成数
            taskUnitFilter.setType("4");
            List<TaskUnitBean> taskUnitList = taskUnitService.queryByCategory(taskUnitFilter);
            int ywcSize = taskUnitList.size();
            //得分
            int scores = 0;
            for (TaskUnitBean taskUnit : taskUnitList) {
                if (StringUtils.isNotBlank(taskUnit.getScore())) {
                    int score = Integer.parseInt(taskUnit.getScore());
                    scores += score;
                }
            }
            //逾期数
            taskUnitFilter.setType(null);
            taskUnitFilter.setStatus("10");
            //完成率
            double d=0;
            if(ywcSize>0){
                String format = df.format((float) ywcSize / (float) allSize * 100);
                d = Double.parseDouble(format);
            }
            int yqSize = taskUnitService.queryByCategory(taskUnitFilter).size();
            map.put("allSize", allSize + "");
            map.put("cbSize", cbSize + "");
            map.put("yqSize", yqSize + "");
            map.put("ywcSize", ywcSize + "");
            map.put("deptName", sysDept.getDeptName());
            map.put("scores", scores);//单位总分
            map.put("wcl", d);//单位完成率
            xianzhidwMapList.add(map);
        }
        if (xianzhidwMapList != null && xianzhidwMapList.size() > 1) {
            Collections.sort(xianzhidwMapList, new Comparator<Map<String, Object>>() {
                @Override
                public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                    Double o1Value = Double.valueOf(o1.get("wcl").toString());
                    Double o2Value = Double.valueOf(o2.get("wcl").toString());
                    return o2Value.compareTo(o1Value);
                }
            });

        }
        //查询当前单位的分数和排名
        for (int i = 0; i < xianzhidwMapList.size(); i++) {
            String deptName = (String) xianzhidwMapList.get(i).get("deptName");
            if (dept.getDeptName().equals(deptName)) {
                deptScore = (int) xianzhidwMapList.get(i).get("scores");
                deptWCL = xianzhidwMapList.get(i).get("wcl")+"%";
                deptSort = i + 1;
            }
        }
        model.addAttribute("sysUser", sysUser);
        model.addAttribute("sysLogin", sysLogin);
        model.addAttribute("dept", dept);
        model.addAttribute("xianzhidwMapList", xianzhidwMapList);
        model.addAttribute("xiangzhendwMapList", xiangzhendwMapList);
        model.addAttribute("deptScore", deptScore);//分数
        model.addAttribute("deptSort", deptSort);//排名
        model.addAttribute("deptWCL", deptWCL);//排名
        return getView("tableTask");
    }


  /*  @RequestMapping("/max")
    public int getMaxValue(){
        int maxValue = taskUnitService.getMaxValue();
        return maxValue;
    }*/
}
